﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using System.Text;
using Newtonsoft.Json;
using ZLCMS.Data.Model;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Payment
{
    public partial class PaymentList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        public string _title;
        protected void Page_Load(object sender, EventArgs e)
        {
            _title = String.IsNullOrEmpty(Request["doctitle"]) ? "" : Request["doctitle"];
            tb_search_title.Text = _title;
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };


            if (_framePage.IsAjaxPagerPostBack)
            {
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                BindList(1, 20);
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopPaymentInfo>();
                SetupCondition(ref criteria);
                criteria.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize).AddOrder(NHibernate.Criterion.Order.Desc("Sort"));

                gridArticle.DataSource = criteria.List();
                gridArticle.DataBind();
            }
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<ShopPaymentInfo>();
                SetupCondition(ref criteria);
                return (int)criteria.SetProjection(Projections.RowCount()).UniqueResult();
            }
        }

        /// <summary>
        /// 设置查询条件
        /// </summary>
        /// <param name="crt"></param>
        protected void SetupCondition(ref ICriteria crt)
        {
            if (!string.IsNullOrEmpty(_title.Trim()))
            {
                crt.Add(Expression.Like("PaymentName", "%" + _title + "%"));
            }
        }
    }
}
